package com.pan.note.service.impl;


import com.alibaba.fastjson.JSON;
import com.pan.note.enumerate.TokenEnum;
import com.pan.note.dao.UserDao;
import com.pan.note.service.LoginService;
import com.pan.note.vo.UserLoginVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 登录请求服务实现类
 */
@Service
@Slf4j
public class LoginServiceImpl implements LoginService {

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 登录请求实现方法
     * @param userLoginVo 登录传参
     * @return 登录结果
     */
    @Override
    public boolean Login(UserLoginVo userLoginVo) {
        //从redis中查询用户
        Map userMap = redisTemplate.opsForHash().entries(TokenEnum.USERKEY.getMsg());
        String userDtoStr = userMap.get(userLoginVo.getAccountId()).toString();
        UserDao userDto = JSON.parseObject(userDtoStr, UserDao.class);
        //比对账号密码
        if (null != userDto && userLoginVo.getUserPassword().equals(userDto.getUserPassword())){
            log.info("用户名:{},密码:{}",userDto.getAccountId(),userDto.getUserPassword());
            log.info("用户比对成功");
            return true;
        }
        log.info("用户名或密码错误");
        return false;
    }

    @Override
    public String getPassword(String accountId) {
        //从redis中查询用户
        Map userMap = redisTemplate.opsForHash().entries(TokenEnum.USERKEY.getMsg());
        String userDtoStr = userMap.get(accountId).toString();
        UserDao userDto = JSON.parseObject(userDtoStr, UserDao.class);
        return userDto.getUserPassword();
    }

    @Override
    public String getUserName(String accountId) {
        //从redis中查询用户
        Map userMap = redisTemplate.opsForHash().entries(TokenEnum.USERKEY.getMsg());
        String userDtoStr = userMap.get(accountId).toString();
        UserDao userDto = JSON.parseObject(userDtoStr, UserDao.class);
        log.info("获取用户名:{}",userDto.getUserName());
        return userDto.getUserName();
    }
}
